{% extends "admin/change_form.html" %}
{% load i18n admin_modify adminmedia cms_tags cms_admin %}
{% block title %}{% trans "Change a page" %}{% endblock %}

{% block extrahead %}
{{ block.super }}
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/csrf.js"></script>
<script type="text/javascript" src="{% url admin:jsi18n %}"></script>

{% if not add %}
<script type="text/javascript" src="{{ STATIC_URL }}cms/js/change_form.js"></script>
{% endif %}
<script type="text/javascript" src="{% admin_static_url %}js/urlify.js"></script>

{% if add %}
<script type="text/javascript">
//<![CDATA[	
(function($) {
	$(document).ready(function (){
		$("#id_title").keyup(function () {
			var e = $("#id_slug")[0];
			if(!e._changed) {
				e.value = URLify(this.value, 64);
			}
	    });
	});
})(jQuery);
//]]>
</script>
{% endif %}

<style type="text/css">
<!--
.language_button { 
	font-weight:bold;
	margin-bottom:-1px;
	border-color:#ddd #aaa #ccc #ddd !important;
	border:1px;
	background:#fff url('{% admin_static_url %}img/admin/nav-bg.gif') repeat-x center bottom !important;
}

#lang_tab_content h2.header {
	margin:0;
	padding:2px 5px 3px 5px;
	font-size:11px;
	text-align:left;
	font-weight:bold;
	background:#7ca0c7 url('{% admin_static_url %}img/admin/default-bg.gif') repeat-x left top;
	color:#fff;
}

.notfilled {
    color: #808080 !important;
}
-->
</style>


{% endblock %}


{% block content_title %}
    <a class="brand">
	{% if moderation_delete_request %}
	{% trans "Approve page deletion" %}

	{% else %}

	{{ title }}
	{% if CMS_MODERATOR %}
		{% if moderation_required %}<span class="moderation-requires-approvement">{% blocktrans %}(requires approvement at {{ moderation_level }} level){% endblocktrans %}</span>
		{% else %}<span>{% trans '(you can perform actions on this page directly)' %}</span>
		{% endif %}
	{% endif %}

	{% endif %}
	</a>
{% endblock %}

{% block object-tools %}
{% if change %}{% if not is_popup %}
<div class="pull-right">
  <div class="object-tools">
	{% if moderation_delete_request %}<a href="remove-delete-state/" class="btn approvelink">{% trans "Remove delete request" %}</a>{% endif %}  	
	{% if moderator_should_approve %}
	<a href="approve/" class="btn approvelink">{% if moderation_delete_request %}{% trans "Approve delete" %}{% else %}{% trans "Approve" %} {% trans "draft" %}{% endif %}</a>
	<a href="{% if current_site_id %}{% ifnotequal current_site_id page.site.id %}http{% if request.is_secure %}s{% endif %}://{{ page.site.domain }}{% endifnotequal %}{% endif %}{{ page.get_absolute_url }}?preview&draft=1" class="btn previewdraftlink">{% trans "Preview" %} {% trans "draft" %}</a>
	{% endif %}
	
	<a href="history/" class="btn historylink">{% trans "History" %}</a>
  	{% if has_absolute_url %}<a href="{% if current_site_id %}{% ifnotequal current_site_id page.site.id %}http{% if request.is_secure %}s{% endif %}://{{ page.site.domain }}{% endifnotequal %}{% endif %}{{ page|preview_link:language }}?preview" class="btn viewsitelink">{% trans "View on site" %}</a>{% endif%}
  </div>
</div>
{% endif %}{% endif %}
{% endblock %}

{% block content %}<div id="content-main">


<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="?language={{ language }}{%if request.GET.target %}&amp;target={{ request.GET.target }}{% endif %}&amp;{%if request.GET.target %}position={{ request.GET.position }}{% endif %}" method="post" id="page_form">{% csrf_token %}{% block form_top %}{% endblock %}
{{ adminForm.fields.parent }}

<div style="display: none;"><input type="hidden" name="language" value="{{language}}" id="id_language"/></div>

{% if show_language_tabs %}
<div id="page_form_lang_tabs">
    {% for lang_code, lang_name in language_tabs %}
        <input type="button" onclick="trigger_lang_button(this,'./?language={{lang_code}}');"
            class="language_button {% ifequal lang_code language %}selected{% else %}{% if not lang_code in filled_languages %} notfilled{% endif %}{% endifequal %}"
            id="debutton" name="{{lang_code}}" value="{{lang_name}}" /> 
    {% endfor %}
</div>
{% endif %}


<div id="lang_tab_content">
{% if show_language_tabs %}
<h2 class="header"></h2>
{% endif %}
{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
{% if save_on_top %}{% submit_row %}{% endif %}
{% if errors %}
    <p class="errornote">
    {% blocktrans count errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
    </p>
    <ul class="errorlist">{% for error in adminform.form.non_field_errors %}<li>{{ error }}</li>{% endfor %}</ul>
{% endif %}

{% for fieldset in adminform %}
  {% include "admin/cms/page/includes/fieldset.html" %}
{% endfor %}


{% for inline_admin_formset in inline_admin_formsets %}
    {% include inline_admin_formset.opts.template %}
{% endfor %}


{% if not add %}
	{% if CMS_PERMISSION and has_change_permissions_permission %}
		<div class="inline-group">
			<div class="tabular inline-related">
				<fieldset id="inherited_permissions" class="module aligned collapse">
				  <h2>{% trans 'All permissions' %}</h2>
				  <div class="loading">{% trans 'Loading...' %}</div>
				  <div class="load">./permissions/</div>
				</fieldset>
			</div>
		</div>
	{% endif %}

	{% if CMS_MODERATOR and has_moderate_permission %}
		<div class="inline-group">
			<div class="tabular inline-related">
				<fieldset id="inherited_permissions" class="module aligned collapse">
				  <h2 class="load_remote">{% trans 'Page states' %}</h2>
				  <div class="loading">{% trans 'Loading...' %}</div>
				  <div class="load">./moderation-states/</div>
				</fieldset>
			</div>
		</div>
	{% endif %}

	{% if moderation_delete_request %}<script type="text/javascript">
		$(function(){
			// disable all fields
			function lockControls(){
				$('input,select,textarea').attr("disabled", "disabled");
				$('a[id^=add_],span[class^=add-plugin],a[class^=selector-],p[class^=selector-]').remove();
			}
			$('fieldset[class=collapsed]').remove();
			lockControls();
			setTimeout(lockControls,200);
		});
	</script>{% endif %}

	{% if CMS_MODERATOR and moderation_required %}
		<div id="dialog" title="Approve dialog">
			<p>{% blocktrans %}This page must be moderated at level {{ moderation_level }}, post a message for moderator.{% endblocktrans %}</p>
			<div class="row"><input type="text" name="df_moderator_message" id="id_df_moderator_message" class="wide" value=""/></div>
			<div class="row"><input type="checkbox" name="df_moderator_state" id="id_df_moderator_state" value="1"/><label for="id_df_moderator_reguest">{% trans 'Request approvemet' %}</label></div>
		</div>

		<script type="text/javascript">
		//<![CDATA[	
		(function($) {
			$(function(){
			
				var submitActor;
				var forceSubmit = false;
				var seen = false;

				// change submit states, open dialog
				$('#page_form').submit(function(event){
					return showDialog(event);
				});

				$('#page_form input[name=_continue]').click(function(event){
					return showDialog(event, this);
				});

				function showDialog(event, actor) {
					if (forceSubmit) return true; // escape sequence

					if (!seen) {
						// show previously posted message if there were an error
						$('#id_df_moderator_message').val($('#id_moderator_message').val());
					}
					seen = true;
					event.preventDefault();
					submitActor = actor;
					$('#dialog').dialog('open');
					return false;
				}

				function dialogSave(){
					// assign values from dialog form to real form
					$('#id_moderator_message').val($('#id_df_moderator_message').val());
					$('#id_moderator_state').val(
						$('#id_df_moderator_state').is(':checked') ? $('#id_df_moderator_state').val() : 0 
					);
					$('#dialog').dialog('close');
					forceSubmit = true;
					$(submitActor || '#page_form input[name=_save]').click();
					return false;
				}

				$('#dialog').dialog({
					bgiframe: true,
					autoOpen: false,
					height: 200,
					width: 400,
					modal: true,
					buttons: {
						Cancel: function() {
							$(this).dialog('close');
						},
						Save: function() {
							dialogSave();
						}
					},
					open: function(){
						var val = $('#id_moderator_state').val();
						$('#id_df_moderator_state').attr('checked', val >= 1 ? 'checked': '');
						$('#id_df_moderator_message')
							.focus()
							.keydown(function(event) { if (event.keyCode == 13) dialogSave()});

						// add default button class to last rendered button (Save)
						$(this.uiDialogButtonPane).find('div.ui-dialog-buttonpane button:last').addClass('default');
					},
					close: function(){
						forceSubmit = false;
					}
				});
			});
		})(jQuery);
		//]]>
		</script>
	{% endif %}

{% endif %}

{% block after_related_objects %}{% endblock %}

{% if add %}
	<div class="submit-row"{% if is_popup %} style="overflow: auto;"{% endif %}>
		<input type="submit" name="_save" class="default" value="{% trans 'Save' %}" {{ onclick_attrib }}/>
		<input type="submit" name="_continue" value="{% trans 'Save and continue editing' %}" {{ onclick_attrib }}/>
	</div>
{% else %}
	{% if not moderation_delete_request %}{% page_submit_row %}{% endif %}
{% endif %}

{% if add %}
   <script type="text/javascript">document.getElementById("{{ adminform.first_field.auto_id }}").focus();</script>
{% endif %}

{# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}

</div>
</form></div>
{% endblock %}
